package org.icress.fusion.shiro

import org.apache.shiro.SecurityUtils

class RoleService {

    static transactional = true

    def save(Role role) {
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        role.validate()
        role.save(flush: true)
    }
    
    def update(Role role) {
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        role.validate()
        role.save(flush: true)
    }
    
    def permissions(grailsApplication) {
        def permissionList = []
        permissionList.add "*:*"
        grailsApplication.controllerClasses.sort{
            it.fullName
        }.each {controller ->
            permissionList << controller.logicalPropertyName + ":*"
            
            controller.reference.propertyDescriptors.each { pd ->
                def closure = controller.getPropertyOrStaticPropertyOrFieldValue(pd.name, Closure)
                if (closure) {
                    if (pd.name != 'beforeInterceptor' && pd.name != 'afterInterceptor') {
                        permissionList << controller.logicalPropertyName + ":" + pd.name + ":*"
                    }
                }
            }
        }
        return permissionList
    }
}
